#include<bits/stdc++.h>
#define ll long long
using namespace std;
int plen,ptop,pstk[40];
char rdc[1<<20],out[1<<20],*rS,*rT;
#define gc() (rS==rT?rT=(rS=rdc)+fread(rdc,1,1<<20,stdin),(rS==rT?EOF:*rS++):*rS++)
#define pc(x) out[plen++]=(x)
#define flush() fwrite(out,1,plen,stdout),plen=0
template<class T=ll>inline T read(){
    T x=0;char ch;bool f=1;
    while(!isdigit(ch=gc()))if(ch=='-')f^=1;
    do x=(x<<1)+(x<<3)+(ch^48);while(isdigit(ch=gc()));
    return f?x:-x;
}
template<class T=ll>inline void write(T x){
	if(plen>=1000000)flush();
	if(!x)return pc('0'),void();
	if(x<0)pc('-'),x=-x;
	for(;x;x/=10)pstk[++ptop]=x%10;
	while(ptop)pc(pstk[ptop--]+'0');
}
inline void write(const char*s){
	if(plen>=1000000)flush();
	for(int i=0;(*(s+i))!='\000';pc(*(s+(i++))));
}
inline void write(char*const s){
	if(plen>=1000000)flush();
	for(int i=0;(*(s+i))!='\000';pc(*(s+(i++))));
}
const int Maxn=2e5+5;
int n,q;
int head[Maxn],to[Maxn<<1],nxt[Maxn<<1],w[Maxn<<1],cnt1;
inline void add(int u,int v,int w1){
	to[++cnt1]=v;nxt[cnt1]=head[u];
	w[cnt1]=w1;head[u]=cnt1;
}
ll dis[Maxn];
int dfn[Maxn],cnt2,top[Maxn],si[Maxn],dfn1[Maxn];
int fa[Maxn],son[Maxn];
inline bool chkin(int x,int u){return (dfn[u]<=dfn[x]&&dfn[x]<dfn[u]+si[u]);}
int dep[Maxn];
void dfs1(int u,int v){fa[u]=v;
	si[u]=1;dep[u]=dep[v]+1;
	for(int i=head[u];i;i=nxt[i]){
		int y=to[i];if(y==v)continue;
		dis[y]=dis[u]+w[i];
		dfs1(y,u);si[u]+=si[y];
		if(si[son[u]]<si[y])son[u]=y;
	}
}
void dfs2(int u,int t){
	top[u]=t;
	dfn[u]=++cnt2;dfn1[cnt2]=u;if(!son[u])return;
	dfs2(son[u],t);
	for(int i=head[u];i;i=nxt[i]){
		int y=to[i];if(y==fa[u]||y==son[u])continue;
		dfs2(y,y);
	}
}
inline int lca(int u,int v){
	while(top[u]^top[v]){
		if(dep[top[u]]<dep[top[v]])swap(u,v);
		u=fa[top[u]];
	}return dep[u]<dep[v]?u:v;
}
int t[Maxn<<2];
void build(int x,int l,int r){
	if(l==r)return void(t[x]=l);
	int mid=l+r>>1;
	build(x<<1,l,mid);build(x<<1|1,mid+1,r);
	t[x]=lca(t[x<<1],t[x<<1|1]);
}
int query(int x,int l,int r,int L,int R){
	if(L<=l&&r<=R)return t[x];
	int mid=l+r>>1;
	if(mid>=L&&mid<R)return lca(query(x<<1,l,mid,L,R),query(x<<1|1,mid+1,r,L,R));
	if(mid>=L)return query(x<<1,l,mid,L,R);
	return query(x<<1|1,mid+1,r,L,R);
}

int root[Maxn],cnt;
struct tree{
	struct Tree{
		int ls,rs,data;
	}t[Maxn*30];
	void change(int&x,int y,int l,int r,int d){
		x=++cnt;t[x]=t[y];++t[x].data;if(l==r)return;
		int mid=l+r>>1;
		if(mid>=d)change(t[x].ls,t[y].ls,l,mid,d);
		else change(t[x].rs,t[y].rs,mid+1,r,d);
	}
	bool query(int x,int y,int l,int r,int L,int R){
		if(L<=l&&r<=R)return t[x].data>t[y].data;
		int mid=l+r>>1;
		if(mid>=L&&query(t[x].ls,t[y].ls,l,mid,L,R))return true;
		return (mid<R&&query(t[x].rs,t[y].rs,mid+1,r,L,R));
	}
}T;

int main(){
	freopen("memory.in","r",stdin);
	freopen("memory.out","w",stdout);
	n=read();q=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read(),w=read();
		add(u,v,w);add(v,u,w);
	}
	dfs1(1,0);dfs2(1,1);
	build(1,1,n);
	for(int i=1;i<=n;i++)T.change(root[i],root[i-1],1,n,dfn[i]);
	ll las=0;
	while(q--){
		ll p=(read()^las),l=(read()^las),r=(read()^las),Lca=query(1,1,n,l,r);
		if(chkin(Lca,p)){
			las=dis[Lca]-dis[p];
			write(las);pc('\n');
			continue;
		}
		if(!chkin(p,Lca)){
			las=dis[p]+dis[Lca]-2*dis[lca(Lca,p)];
			write(las);pc('\n');
			continue;
		}
		int u=p;
		while(fa[top[u]]&&!T.query(root[r],root[l-1],1,n,dfn[fa[top[u]]],dfn[fa[top[u]]]+si[fa[top[u]]]-1))u=fa[top[u]];
		int L=1,R=dep[u]-dep[top[u]],mid,res=u;
		
		while(L<=R){
			mid=L+R>>1;
			int x=dfn1[dfn[u]-mid];
			if(!T.query(root[r],root[l-1],1,n,dfn[x],dfn[x]+si[x]-1))res=x,L=mid+1;
			else R=mid-1;
		}
		u=res;
		if(!T.query(root[r],root[l-1],1,n,dfn[u],dfn[u]+si[u]-1))u=fa[u];
		las=dis[p]-dis[u];
		write(las);pc('\n');
	}flush();
	return 0;
}

